# -*- coding: utf-8 -*-
# @Time    : 2017/11/3 11:32
# @Author  : Deyu.Tian
# @Site    :
# @File    : config.py
# @Software: PyCharm Community Edition


from __future__ import unicode_literals

import config
import util
from geotiff_tools import img2array
import geotiff_tools as imt
import os

import numpy as np

layername = {'WEASD', 'SNOD'}

def clip_tif(datadir, outdir):
    """
    tif clip to interest extent
    :param datadir:
    :param outdir:
    :return:
    """
    files = util.list_all_tifs(datadir)
    for fname in files:
        cmd = "gdalwarp -te 74.2413390032 38.0294935187 75.746648156 39.2232617758 " \
              "{} {}/{}.tif".format(fname, outdir, fname[-11:-4])
        os.system(cmd)



def trans_clip_ncar(datadir, outdir):
    """
    translate .nc to tif and clip to interest extent
    :param datadir:
    :param outdir:
    :return:
    """
    files = util.list_all_ncs(datadir)
    #print files
    for fname in files:
        for layer in layername:
            cmd = "gdal_translate -projwin 74.3813390032 39.1232617758 75.616648156 38.1294935187 " \
                  "NETCDF:'{}':{}_P0_L1_GLL0 {}/{}_{}_surface.tif"\
                  .format(fname, layer, outdir, fname.split("/")[-1].split(".")[2], layer)
            os.system(cmd)
    pass

def avg_Che_SNOD_of_area(tifdir, csvdir):
    """
    for Che snow depth
    :param tifdir:
    :param csvdir:
    :return:
    """
    files = util.list_all_ncs(tifdir, pattern='*', ext='tif')
    avg_SNOD = np.zeros(len(files))
    for i, fname in enumerate(files):
        arr = img2array(fname)
        avg_SNOD[i] = np.average(arr)
    np.savetxt("{}/avg_SNOD_over_area.csv".format(csvdir), avg_SNOD)

def avg_SNOD_of_time(tifdir, outdir):
    """
    for Che snow depth
    :param tifdir:
    :param csvdir:
    :return:
    """
    files = util.list_all_ncs(tifdir, pattern='*', ext='tif')
    imggt, b = imt.read_tif_metadata(files[0])
    arr = np.zeros_like(b)
    for i, fname in enumerate(files):
        arr += img2array(fname)
    arr = arr / len(files)
    imt.array2rasterwgs84("{}/Che_SD_avg_time.tif".format(outdir), imggt, arr)



def avg_Ncar_SNOD_of_area(tifdir, csvdir):
    """
    for NCAR snow depth
    :param tifdir:
    :param csvdir:
    :return:
    """
    files = util.list_all_ncs(tifdir, pattern='*00_SNOD_surface', ext='tif')
    avg_SNOD = np.zeros(len(files))
    for i, fname in enumerate(files):
        arr = img2array(fname)
        avg_SNOD[i] = np.average(arr)
    np.savetxt("{}/avg_SNOD_over_area.csv".format(csvdir), avg_SNOD)

def avg_WEASD_of_area(tifdir, csvdir):
    """
    for NCAR snow water depth
    :param tifdir:
    :param csvdir:
    :return:
    """
    files = util.list_all_ncs(tifdir, pattern='*00_WEASD_surface', ext='tif')
    avg_WEASD = np.zeros(len(files))
    day = np.zeros(len(files))
    for i, fname in enumerate(files):
        arr = img2array(fname)
        avg_WEASD[i] = np.average(arr)
        day[i] = int(fname.split('/')[-1].split('_')[0])
    np.savetxt("{}/avg_WEASD_over_area_2015-2016.csv".format(csvdir), avg_WEASD)
    np.savetxt("{}/timedays_2015-2016.csv".format(csvdir), day)

def avg_DAY_TMP_of_area(tifdir, csvdir):
    files = util.list_all_ncs(tifdir, pattern='*12_TMP_surface', ext='tif')
    avg_TMP = np.zeros(len(files))
    #day = np.zeros(len(files))
    for i, fname in enumerate(files):
        arr = img2array(fname)
        avg_TMP[i] = np.average(arr)
        #day[i] = int(fname.split('/')[-1].split('_')[0][4:8])
    np.savetxt("{}/avg_DAY_TMP_over_area.csv".format(csvdir), avg_TMP)
    #np.savetxt("{}/timedays.csv".format(csvdir), day)


if __name__ == '__main__':
    basedir = "{}/atmos_related_experiments/NCAR/rda_request_274039".format(config.shareDir)
    #tifdir = "{}/atmos_related_experiments/NCAR/rda_request_274039tifs".format(config.shareDir)
    csvdir = "{}/atmos_related_experiments/NCAR/rda_request_274039outfile".format(config.shareDir)
    tifdir = "/home/deyu1/share/gazreasarch/optical_related_experiments/atmos_related_experiments/Che/MODELLING"
    outdir = "/home/deyu1/share/gazreasarch/optical_related_experiments/atmos_related_experiments/Che"
    #tifdir = "/home/deyu1/share/gazreasarch/optical_related_experiments/atmos_related_experiments/NCAR/modellingusage"
    #outdir = "/home/deyu1/share/gazreasarch/optical_related_experiments/atmos_related_experiments/NCAR"
    avg_SNOD_of_time(tifdir, outdir)
    #csvdir = "P:\Aoi_paper\data\snow\Che\\analysis"
    #clip_tif(tifdir, tifdir)
    #trans_clip_ncar(basedir, tifdir)
    #avg_Ncar_SNOD_of_area(tifdir, csvdir)
    #trans_clip_ncar(basedir, tifdir)
    #csvdir = "{}/atmos_related_experiments/NCAR/outfile".format(config.shareDir)
    #avg_WEASD_of_area(outdir, csvdir)
    #avg_SNOD_of_area(tifdir, csvdir)
